
import React,{Component}from 'react';
import { TouchableOpacity, Text } from 'react-native';
import QRCodeScanner from 'react-native-qrcode-scanner';
import { RNCamera } from 'react-native-camera';

export default class ScanCodepage extends Component {
    constructor(props) {
        super(props);
        this.state = {
            hasPermission: false,
            result: ''

        };
        this.requestCameraPermission = this.requestCameraPermission.bind(this)




    }
    componentWillMount(){
        this.requestCameraPermission();
    }

    componentDidMount() {
        // const that = this

        //     (async () => {


        //     })();



    }

    //请求权限的方法
    async requestCameraPermission() {
        try {
            const granted = await PermissionsAndroid.request(
                PermissionsAndroid.PERMISSIONS.CAMERA,
                {
                    title: '申请摄像头权限',
                    message:
                        '一个很牛逼的应用想借用你的摄像头，' +
                        '然后你就可以拍出酷炫的皂片啦。',
                    buttonNeutral: '等会再问我',
                    buttonNegative: '不行',
                    buttonPositive: '好吧',
                },
            );
            if (granted === PermissionsAndroid.RESULTS.GRANTED) {
                console.log('现在你获得摄像头权限了');
            } else {
                console.log('用户并不屌你');
                this.props.navigation.goBack()
            }
        } catch (err) {
            console.warn(err);
        }
    }

    componentDidUpdate() {


    }

    onSuccess = e => {
        console.log(e)

        var callback = this.props.route.params.callBack
        callback(e.data+"")

        this.props.navigation.pop()


        this.setState({result: e.data})
    };



    render() {




        return (
            <QRCodeScanner
                onRead={this.onSuccess}
                flashMode={RNCamera.Constants.FlashMode.torch}
                topContent={
                    <Text>
                         scan the QR code.
                      
                    </Text>
                }
                bottomContent={
                    <TouchableOpacity >
                        <Text >{this.state.result}</Text>
                    </TouchableOpacity>
                }
            />

        )
    }






}